package com.cyneck.demo.constant;

import java.awt.*;
import java.util.Arrays;
import java.util.Optional;

/**
 * <p>Description: </p>
 *
 * @author Eric Lee
 * @version v1.0.0
 * @since 2020/9/4 18:18
 **/
public enum ColorEnumFactory {


    WHITE {
        @Override
        public Color create() {
            return Color.white;
        }
    },


    BLUE {
        @Override
        public Color create() {
            return new Color(66, 142, 218);
        }
    },

    RED {
        @Override
        public Color create() {
            return new Color(200, 0, 0);
        }
    },

    GREEN {
        @Override
        public Color create() {
            return new Color(30, 83, 126);
        }
    },

    GRAY {
        @Override
        public Color create() {
            return new Color(138, 144, 165);
        }
    };


    //枚举内的抽象方法
    public abstract Color create();


    /**
     * 根据传参构建实例
     *
     * @param code 名字跟枚举类一样（忽略大小写）
     * @return
     */
    public static Color create(String code) {
        Optional<ColorEnumFactory> colorEnumFactoryOptional = Arrays.stream(
                ColorEnumFactory.values()).filter(s -> s.name().equals(code.toUpperCase())).findFirst();
        return colorEnumFactoryOptional.isPresent() ? colorEnumFactoryOptional.get().create() : null;

    }
}
